package flst.ipiweb.common;

import java.util.Calendar;
import java.sql.Date;
import java.util.List;

public class PromotionalCode {

	private int id;
	private String code;
	private TypeCode type;
	private List<Article> listArticles;
	private Article article;
	private SpecialOfferEnum kindOfOffer;
	private float percentage;
	private float amount;
	private Date beginDate;
	private Date endDate;
	private Shop shop;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public SpecialOfferEnum getKindOfOffer() {
		return kindOfOffer;
	}

	public void setKindOfOffer(SpecialOfferEnum kindOfOffer) {
		this.kindOfOffer = kindOfOffer;
	}

	public float getPercentage() {
		return percentage;
	}

	public void setPercentage(float percentage) {
		this.percentage = percentage;
	}

	public float getAmount() {
		return amount;
	}

	public void setAmount(float amount) {
		this.amount = amount;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public float getBillDiscount(Command command) {

		if (command != null
				&& Calendar.getInstance().getTime().getTime() > this
						.getBeginDate().getTime()
				&& Calendar.getInstance().getTime().getTime() < this
						.getEndDate().getTime()
				&& command.getCommandLineList() != null
				&& command.getCommandLineList().isEmpty() == false) {

			float discount = 0;

			if (this.kindOfOffer == SpecialOfferEnum.AMOUNT_OFF_AN_ARTICLE
					&& this.getListArticles() != null
					&& this.getListArticles().isEmpty() == false) {

				for (Article article : this.getListArticles()) {
					for (CommandLine line : command.getCommandLineList()) {
						if (line.getArticle() != null
								&& line.getArticle().equals(article)) {

							discount += line.getQuantity() * this.getAmount();

						}
					}
				}

			} else if (this.kindOfOffer == SpecialOfferEnum.AMOUNT_OFF_THE_BILL) {
				discount += this.getAmount();
			} else if (this.kindOfOffer == SpecialOfferEnum.PERCENT_OFF_AN_ARTICLE
					&& this.getListArticles() != null
					&& this.getListArticles().isEmpty() == false) {

				for (Article article : this.getListArticles()) {
					for (CommandLine line : command.getCommandLineList()) {
						if (line.getArticle() != null
								&& line.getArticle().equals(article)) {

							discount += line.getQuantity() * article.getPrice()
									* this.getPercentage() / 100;

						}
					}
				}

			}else if(this.kindOfOffer == SpecialOfferEnum.PERCENT_OFF_THE_BILL){
				discount += command.getCommandPrice() * this.getPercentage() / 100;
			}

			return discount;

		} else {
			return 0;
		}
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public TypeCode getType() {
		return type;
	}

	public void setType(TypeCode type) {
		this.type = type;
	}

	public List<Article> getListArticles() {
		return listArticles;
	}

	public void setListArticles(List<Article> listArticles) {
		this.listArticles = listArticles;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

}
